package apptoko.com.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import apptoko.com.model.ContentItemInfo.RelatedItem;

import com.androidquery.AQuery;
import apptoko.com.R;

public class RelatedItemAdapter extends ArrayAdapter<RelatedItem> {
	
	private LayoutInflater inflater;
	private AQuery aq;

	public RelatedItemAdapter(Context context, int resource, List<RelatedItem> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.related_item, null);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.related_icon);
			holder.name = (TextView) convertView.findViewById(R.id.related_name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		RelatedItem item = getItem(position);
		if(item != null){
			aq = new AQuery(convertView);
			aq.id(holder.icon).image(item.getIconUrl(), false, true, 0, 0, null, AQuery.FADE_IN_NETWORK);
			aq.id(holder.name).text(item.getName());
		}
		return convertView;
	}
	public static class ViewHolder {
		ImageView icon;
		TextView name;
	}
}